/*
 * Copyright (c) 2022.版权所有高金磊
 */

package 领扣算法.A简单题.从根到叶的二进制数之和;

public class Main {
  int res=0;

    public int sumRootToLeaf(TreeNode root) {
        if (root!=null)
            plus(root,0);
     return res;
    }

    private void plus(TreeNode root,int num) {
        num*=2;
        num+=root.val;
        if (root.right==root.left)
        {
            res+=num;
            return;
        }
        if (root.left!=null)
            plus(root.left, num);
        if (root.right!=null)
            plus(root.right, num);

    }

 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
